package com.ce.source.analysis.http;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：前世小和尚（xiaopingzhang8@creditease.cn）
 * @Description ：
 * @ClassName ：HttpClientProxy
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2018/4/2 12:55
 */
public class HttpClientProxy {

    public static String execPost(String url, Map<String,Object> param) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
//        for(Map.Entry<String,Object> entry:param.entrySet()){
//            list.add(new BasicNameValuePair(entry.getKey(),entry.getKey()));
//        }
        HttpEntity entity = new StringEntity(JSON.toJSONString(param),"application/json", "utf-8");
        post.setEntity(entity);
        HttpResponse response = httpClient.execute(post);

        return EntityUtils.toString(response.getEntity());
    }

    public static void main(String[] args) {
        Map<String, Object> parames = new HashMap<String, Object>();
        try {
            String url = "http://baidu.com";
            parames.put("pageSize","10");
            parames.put("pageNum","1");
            parames.put("system","yixinonline");
            parames.put("userId","10014330");
            System.out.print(execPost(url, parames));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
